package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessFarmSystem;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.AddressVO;
import com.digitalchina.lxnsy.interfaces.IAddressCallBack;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.MyAddressListHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/8/3 17:47
 * 修改备注：
 * ================================================
 */
public class MyAddressListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, IAddressCallBack {
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    private RecyclerArrayAdapter adapter;
    private List<AddressVO> mList = new ArrayList<>();
    private boolean isShowCheck = false;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_my_address);
        setTitle("地址列表");
    }

    @Override
    public void initMember() {

        if (getIntent().hasExtra("isShowCheck"))
            isShowCheck = true;
        else
            isShowCheck = false;

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new MyAddressListHolder(parent, isShowCheck, MyAddressListActivity.this);
            }
        });


        onRefresh();

    }

    @Override
    public void addListener() {

        recyclerView.setRefreshListener(this);


        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_ADDRESS_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_ADDRESS_LIST_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_ADDRESS_DELETE_SUCCESS:
                        onRefresh();
                        showToast("删除成功");
                        break;
                    case MSG.MSG_ADDRESS_DELETE_FIELD:
                        showToast(msg.obj.toString());
                        break;
//                    case MSG.MSG_ADDRESS_DELETE_SUCCESS:
//                        mAdapter.getListData().remove(delAddressPosition);
//                        mAdapter.notifyDataSetChanged();
//                        showToast("地址删除成功");
//                        break;
//                    case MSG.MSG_ADDRESS_DELETE_FIELD:
//                        showToast(msg.obj.toString());
//                        break;
//                    case MSG.MSG_ADDRESS_DEFAULT_SUCCESS:
//                        BusinessAddress.getAddressList(MyAddressActivity.this, mHandler);
//                        break;
//                    case MSG.MSG_ADDRESS_DEFAULT_FIELD:
//                        showToast(msg.obj.toString());
//                        break;

                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        mList = FastJsonUtil.getListBean(object.toString(), "body", "addressList", AddressVO.class);

        adapter.clear();
        adapter.addAll(mList);

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.my_address_rl_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_address_rl_add:
                GotoUtil.gotoForResultActivity(MyAddressListActivity.this, EditAddressActivity.class, 99);
                break;
        }
    }

    @Override
    public void onRefresh() {
        BusinessFarmSystem.getAddressList(this, mHandler);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
            onRefresh();

    }

    @Override
    public void onlinePreviewClick(int pos) {
        AddressVO addressVO = (AddressVO) adapter.getAllData().get(pos);
        BusinessFarmSystem.deleteAddress(this, addressVO.getId(), mHandler);
    }

    @Override
    public void onCheckView(int pos) {
        Intent intent = new Intent();
        intent.putExtra("addressVO", (AddressVO) adapter.getAllData().get(pos));
        setResult(RESULT_OK, intent);
        finish();
    }
}
